<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Minim : : BitCrush</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="stylesheet.css" rel="stylesheet" type="text/css">
</head>
<body> 
<center>
<table class="mainTable">
  <tr>
    <td class="header">
    	<span class="indexheader">Minim</span><br/>
    	<span class="indexnavigation">
    		<a href="index.html">core</a> | 
    		<a href="index_ugens.html">ugens</a> | 
    		<a href="index_analysis.html">analysis</a>
    	</span>
    </td>
    <td class="border-left">&nbsp;</td>
  </tr>
  
  <tr>
    <td class="classNavigation">
    	<p class="mainTextName">BitCrush</p>
    	
       <p class="linkListHeader">Fields</p>
       <p class="linkList">
           
    <a href="bitcrush_field_audio.html" title="The audio input is where audio that gets bit-crushed should be patched. 
 However, you don't need to patch directly to this input, patching to
 the UGen itself will accomplish the same thing.">audio</a><br/>
    
    <a href="bitcrush_field_bitrate.html" title="Control the bit rate with another UGen by patching to bitRate.
 Values that make sense for this start at 1 and go up to whatever the
 sample rate of your AudioOutput are (typically 44100)">bitRate</a><br/>
    
    <a href="bitcrush_field_bitres.html" title="Control the bit resolution with another UGen by patching to bitRes. Values that 
 make sense for this start at 1 and go up to whatever the actual resolution of 
 the incoming audio is (typically 16).">bitRes</a><br/>
    
	   </p>
   
    	
       <p class="linkListHeader">Methods</p>
       <p class="linkList">
           
    <a href="bitcrush_method_channelcount.html" title="Returns the number of channels this UGen has been configured to generate.">channelCount ( )</a><br/>
    
    <a href="bitcrush_method_getlastvalues.html" title="Return the last values generated by this UGen. This will most often be
 used by sub-classes when pulling data from their inputs.">getLastValues ( )</a><br/>
    
    <a href="bitcrush_method_patch.html" title="Send the output of this UGen to another UGen, UGenInput, or AudioOutput.">patch ( )</a><br/>
    
    <a href="bitcrush_method_printinputs.html" title="Prints all inputs connected to this UGen (for debugging)">printInputs ( )</a><br/>
    
    <a href="bitcrush_method_samplerate.html" title="Returns the sample rate of this UGen.">sampleRate ( )</a><br/>
    
    <a href="bitcrush_method_setbitres.html" title="Set the bit resolution directly.">setBitRes ( )</a><br/>
    
    <a href="bitcrush_method_setchannelcount.html" title="Let this UGen know how many channels of audio you will be asking it for.">setChannelCount ( )</a><br/>
    
    <a href="bitcrush_method_setsamplerate.html" title="Set the sample rate for this UGen.">setSampleRate ( )</a><br/>
    
    <a href="bitcrush_method_tick.html" title="Generates one sample frame for this UGen.">tick ( )</a><br/>
    
    <a href="bitcrush_method_unpatch.html" title="Unpatch this UGen from an AudioOutput or other UGen.">unpatch ( )</a><br/>
    
	   </p>
   
    </td>
    <td class="mainText border-left">
    	BitCrush is an effect that reduces the fidelity of the incoming signal.
 This results in a sound that is "crunchier" sounding, or "distorted". 
 <p>
 Audio is represented digitally (ultimately) as an integral value. If you 
 have 16-bit audio, then you can represent a sample value with any number 
 in the range -32,768 to +32,767. If you bit-crush this audio to be 8-bit,
 then you effectively reduce it representation to -128 to +127, even though 
 you will still represent it with a 16-bit number. This reduction in the 
 fidelity of the representation essentially squares off the waveform, 
 which makes it sound "crunchy". Try bit crushing down to 1-bit and see 
 what you get!
    	<p class="memberSectionHeader">Constructors</p>
    	<pre><em>Construct a BitCrush with a bit resolution of 1 and a bit rate of 44100.</em>
BitCrush()
<em>Construct a BitCrush with the specified bit resolution and bit rate.</em>
BitCrush(float localBitRes, float localBitRate)
</pre>
    	
   <p class="memberSectionHeader">Parameters</p>
   
        <span class="parameterName">localBitRes</span>&nbsp;&mdash;&nbsp;<span class="parameterDescription">float: typically you'll want this in the range [1,16]</span><br/>
    
        <span class="parameterName">localBitRate</span>&nbsp;&mdash;&nbsp;<span class="parameterDescription">float: this must be in the range [1,outputSampleRate]</span><br/>
    
   
    	<p class="memberSectionHeader">Related</p>
    	<A href="ugen_class_ugen.html">UGen</A><BR>

    	<p class="memberSectionHeader">Example</p>
    	<pre>/* bitCrushExample&lt;br/>
 * This is an example of using a BitCrush UGen to modify the sound of an Oscil.
 * &lt;p>
 * For more information about Minim and additional features, 
 * visit http://code.compartmental.net/minim/
 */

import ddf.minim.*;
import ddf.minim.ugens.*;

Minim minim;
AudioOutput out;

// this CrushInstrument will play a sine wave bit crushed
// to a certain bit resolution. this results in the audio sounding
// "crunchier".
class CrushInstrument implements Instrument
{
  Oscil sineOsc;
  BitCrush bitCrush;
  
  CrushInstrument(float frequency, float amplitude, float bitRes)
  {
    sineOsc = new Oscil(frequency, amplitude, Waves.SINE);
    
    // BitCrush takes the bit resolution for an argument
    bitCrush = new BitCrush(bitRes, out.sampleRate());
    
    sineOsc.patch(bitCrush);
  }
  
  // every instrument must have a noteOn( float ) method
  void noteOn(float dur)
  {
    bitCrush.patch(out);
  }
  
  // every instrument must have a noteOff() method
  void noteOff()
  {
    bitCrush.unpatch(out);
  }
}

// this CrushingInstrument will play a sine wave and then change the bit resulution of the BitCrush
// over time, based on a starting and ending resolution passed in.
class CrushingInstrument implements Instrument
{
  Oscil sineOsc;
  BitCrush bitCrush;
  Line crushLine;
  
  CrushingInstrument(float frequency, float amplitude, float hiBitRes, float loBitRes)
  {
    sineOsc = new Oscil(frequency, amplitude, Waves.SINE);
    bitCrush = new BitCrush(hiBitRes, out.sampleRate());
    crushLine = new Line(9.0, hiBitRes, loBitRes);
    
    // our Line will control the resolution of the bit crush
    crushLine.patch(bitCrush.bitRes);
    // patch the osc through the bit crush
    sineOsc.patch(bitCrush);
  }
  
  // called by the note manager when this instrument should play
  void noteOn(float dur)
  {
    // patch the bit crush to the output and active our Line when we want to have the note play
    crushLine.activate();
    bitCrush.patch(out);
  }
  
  // called by the note manager when this instrument should stop playing
  void noteOff()
  {
    // unpatch from the output to stop making sound
    bitCrush.unpatch(out);
  }
}

void setup()
{
  // initialize the drawing window
  size( 512, 200, P2D );

  // initialize the minim and out objects
  minim = new Minim( this );
  out = minim.getLineOut( Minim.MONO );
  
  // queue up some notes using the Crush Instrument
  // its arguments are sine wave frequency, amplitude, and bit crush resolution
  out.playNote(0.5, 2.6, new CrushInstrument( 392.0, 0.5, 16.0) );
  out.playNote(3.5, 2.6, new CrushInstrument( 370.0, 0.5, 4.0) );
  out.playNote(6.5, 2.6, new CrushInstrument( 261.6, 0.5, 3.0) );
  out.playNote(9.5, 2.6, new CrushInstrument( 247.0, 0.5, 2.0) );
  
  // queue up a Crushing Instrument, which will change the bit resolution over time
  // its arguments are sine frequency, amplitude, bit crush resolution start and end
  out.playNote(12.5, 10.0, new CrushingInstrument( 191.0, 0.5, 5.2, 1.0 ) );
}

// draw is run many times
void draw()
{
  // erase the window to black
  background( 0 );
  // draw using a white stroke
  stroke( 255 );
  // draw the waveforms
  for( int i = 0; i &lt; out.bufferSize() - 1; i++ )
  {
    // find the x position of each buffer value
    float x1  =  map( i, 0, out.bufferSize(), 0, width );
    float x2  =  map( i+1, 0, out.bufferSize(), 0, width );
    // draw a line from one buffer position to the next for both channels
    line( x1, 50 + out.left.get(i)*50, x2, 50 + out.left.get(i+1)*50);
    line( x1, 150 + out.right.get(i)*50, x2, 150 + out.right.get(i+1)*50);
  }  
}
</pre>
    	<p class="memberSectionHeader">Usage</p>
    	Web & Application
    </td>
  </tr>
</table>
</center>
</body>
</html>